package com.xiaoshuidi.cloud.module.member.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.member.controller.admin.comment.vo.CommentPageVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.comment.vo.CommentStatisticsReqVo;
import com.xiaoshuidi.cloud.module.member.controller.admin.comment.vo.CommentStewardPageReqVo;
import com.xiaoshuidi.cloud.module.member.controller.admin.comment.vo.CommentStewardPageVo;
import com.xiaoshuidi.cloud.module.member.controller.app.comment.vo.MemberCommentCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.app.comment.vo.MemberCommentRespVO;
import com.xiaoshuidi.cloud.module.member.mapper.comment.MemberCommentMapper;
import com.xiaoshuidi.cloud.module.member.pojo.comment.MemberComment;
import com.xiaoshuidi.cloud.module.system.api.user.AdminUserApi;
import com.xiaoshuidi.cloud.module.system.api.user.dto.AdminUserRespDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
@Slf4j
public class MemberCommentRepository extends ServiceImpl<MemberCommentMapper, MemberComment> {
    private final MemberCommentMapper mapper;
    private final AdminUserApi adminUserApi;

    /**
     * 此方法默认新增已评的评价内容
     *
     * @param vo
     * @return
     */
    public Boolean create(MemberCommentCreateVO vo) {
        MemberComment memberComment = new MemberComment();
        BeanUtils.copyProperties(vo, memberComment);
        memberComment.setStatus(true);
        mapper.insert(memberComment);
        return true;
    }

    /**
     * 新增未评价信息
     *
     * @return
     */
    public Boolean createUnComment(MemberComment memberComment) {
        memberComment.setStatus(false);
        mapper.insert(memberComment);
        return true;
    }

    public MemberCommentRespVO getCommentById(Long id) {
        MemberCommentRespVO memberCommentRespVO = new MemberCommentRespVO();
        MemberComment memberComment = mapper.selectById(id);
        BeanUtils.copyProperties(memberComment, memberCommentRespVO);
        return memberCommentRespVO;
    }

    public List<MemberCommentRespVO> getListByUserId(Long userId, Boolean status) {
//        LambdaQueryWrapper<MemberComment> wrapper = new LambdaQueryWrapper<MemberComment>()
//                .eq(MemberComment::getCreator, userId)
//                .eq(MemberComment::getStatus, status)
//                .orderByDesc(MemberComment::getCreateTime);
//        return mapper.selectList(wrapper);
        return mapper.getListByUserId(userId,status);
    }


    public List<MemberComment> getCommentStatistics(CommentStatisticsReqVo reqVo) {
        return mapper.getCommentStatistics(reqVo);
    }

    public IPage<MemberCommentRespVO> getCommentPage(CommentPageVO pageVO) {
        IPage<MemberCommentRespVO> page = mapper.getCommentPage(new Page<MemberComment>(pageVO.getPageNo(), pageVO.getPageSize()), pageVO);
        return page;
    }

    public IPage<CommentStewardPageVo> getCommentStewardPage(CommentStewardPageReqVo pageVO) {
        IPage<CommentStewardPageVo> page = mapper.getCommentStewardPage(new Page<MemberComment>(pageVO.getPageNo(), pageVO.getPageSize()), pageVO);
        return page;
    }

    public MemberCommentRespVO getByMessageId(Long id) {
        MemberCommentRespVO memberCommentRespVO = new MemberCommentRespVO();
        LambdaQueryWrapper<MemberComment> wrapper = new LambdaQueryWrapper<MemberComment>()
                .eq(MemberComment::getMessageId, id);
        List<MemberComment> memberComments = mapper.selectList(wrapper);
        if (memberComments != null && memberComments.size() > 0) {
            BeanUtils.copyProperties(memberComments.get(0), memberCommentRespVO);

        }
        return memberCommentRespVO;
    }

    public MemberComment getCommentByMessageId(Long messageId) {
        LambdaQueryWrapper<MemberComment> wrapper = new LambdaQueryWrapper<MemberComment>()
                .eq(MemberComment::getMessageId, messageId);
        List<MemberComment> memberComments = mapper.selectList(wrapper);
        if (memberComments != null && memberComments.size() > 0) {
            return memberComments.get(0);
        }
        return null;
    }
}
